package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

type Department struct {
	Id         int64     `orm:"pk;auto"`   // 项目ID
	Title      string    `orm:"size(255)"` // 日志标题
	ParentId   int64     `orm:"size(11)"`  // 项目标题模块
	Describe   string    `orm:"size(255)"` //描述
	CreateAt   time.Time `orm:"auto_now_add;type(datetime)"form:"-"`
	CreateBy   int64     `orm:"size(11)"` //用户名称
	UpdateAt   time.Time `orm:"auto_now;type(datetime)"`
	UpdateName string    `orm:"size(20)"` //
	CreateName string    `orm:"size(20)"` //更新者姓名
	UpdateBy   int64     `orm:"size(11)"` //更新者姓名
}

// 获取模型名称
// 获取模型表名
func (m *Department) TableName() string {
	return "department"
}

// 查询方法
func (m *Department) Query() orm.QuerySeter {
	return orm.NewOrm().QueryTable(m)
}

// 添加用户
func (m *Department) Insert() error {
	_, err := orm.NewOrm().Insert(m)
	return err
}

// 删除用户
func (m *Department) Delete() error {
	_, err := orm.NewOrm().Delete(m)
	return err
}

// 修改用户信息
func (m *Department) Update(fields ...string) error {
	_, err := orm.NewOrm().Update(m, fields...)
	return err
}

func (m *Department) GetObjectById(id int64) (*Department, error) {
	err := m.Query().Filter("id", id).One(m)
	return m, err
}

func (m *Department) GetEleListsById(uid int64) []map[string]interface{} {
	list := make([]*Department, 0)
	_, _ = m.Query().Filter("id", uid).All(&list)
	resTree := make([]map[string]interface{}, 0)
	for _, citem := range list {
		cres := DepartmentJson(citem)
		resTree = append(resTree, cres)
	}
	return resTree
}

func (m *Department) GetList() []map[string]interface{} {
	list := make([]*Department, 0)
	_, _ = m.Query().All(&list)
	resTree := make([]map[string]interface{}, 0)
	for _, citem := range list {
		cres := DepartmentJson(citem)
		resTree = append(resTree, cres)
	}
	return resTree
}
